package com.ebt.m.customer.fakeDialog.drawable;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;

public class FakeDialogBackgoundDrawable extends Drawable {

	private static final int COLORDRAWABLE_DIMENSION = 2;
	private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
	private Paint mPaint;
	private Bitmap mBitmap;

	private RectF mRectBounds;

	public FakeDialogBackgoundDrawable(Bitmap bitmap) {
		mBitmap = bitmap;
		BitmapShader bitmapShader = new BitmapShader(bitmap, TileMode.CLAMP,
				TileMode.CLAMP);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setShader(bitmapShader);
	}

	public FakeDialogBackgoundDrawable(Drawable drawable) {
		mBitmap = getBitmapFromDrawable(drawable);
		BitmapShader bitmapShader = new BitmapShader(mBitmap, TileMode.CLAMP,
				TileMode.CLAMP);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setShader(bitmapShader);
	}
	
	float mRadius = 0;
	@Override
	public void setBounds(int left, int top, int right, int bottom) {
		super.setBounds(left, top, right, bottom);
		mRectBounds = new RectF(left, top, right, bottom);
		mRadius = (float)Math.sqrt(mRectBounds.width() * mRectBounds.width() + mRectBounds.height() * mRectBounds.height());
	}
	
	float currRadius = 0;

	public void setRadius(float radius){
		if(currRadius == radius){
			return;
		}
		currRadius = radius;
		invalidateSelf();
	}
	
	float currRatio = 0;
	public void setRadiusRatio(float ratio){
		if(currRatio == ratio){
			return;
		}
		currRadius = ratio * mRadius;
		invalidateSelf();
	}
	
	@Override
	public void draw(Canvas canvas) {
		canvas.drawCircle(mRectBounds.width(), mRectBounds.height(),
				currRadius, mPaint);
	}

	@Override
	public int getIntrinsicWidth() {
		return mBitmap.getWidth();
	}

	@Override
	public int getIntrinsicHeight() {
		return mBitmap.getHeight();
	}

	@Override
	public void setAlpha(int alpha) {
		mPaint.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
		mPaint.setColorFilter(cf);
	}

	@Override
	public int getOpacity() {
		return PixelFormat.TRANSLUCENT;
	}

	public static Bitmap getBitmapFromDrawable(Drawable drawable) {
		if (drawable == null) {
			return null;
		}

		if (drawable instanceof BitmapDrawable) {
			return ((BitmapDrawable) drawable).getBitmap();
		}

		try {
			Bitmap bitmap;

			if (drawable instanceof ColorDrawable) {
				bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION,
						COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
			} else {
				bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(), BITMAP_CONFIG);
			}

			Canvas canvas = new Canvas(bitmap);
			drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
			drawable.draw(canvas);
			return bitmap;
		} catch (OutOfMemoryError e) {
			return null;
		}
	}

}
